﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;


namespace AnyOffice.general.system.code
{
    public partial class _new : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                string str = (Request.QueryString["code_flag"] != null) ? Request.QueryString["code_flag"].ToString() : "";
                this.ViewState["CodeFlag"] = str;
                string commandText = "SELECT * FROM [dbo].[Sys_Code] WHERE [CodeNo]=@CodeFlag AND [CodeFlag] IS NULL";
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@CodeFlag", SqlDbType.NVarChar, 200) };
                commandParameters[0].Value = str;
                DataTable table = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText, commandParameters).Tables[0];
                if (table.Rows.Count > 0)
                {
                    this.litCodeName.Text = table.Rows[0]["CodeName"].ToString();
                }
            }
            if (this.Page.IsPostBack)
            {
                string str3 = "SELECT * FROM [dbo].[Sys_Code] WHERE [CodeFlag]=@CodeFlag AND [CodeNo]=@CodeNo";
                SqlParameter[] parameterArray2 = new SqlParameter[2];
                parameterArray2[0] = new SqlParameter("@CodeFlag", SqlDbType.NVarChar, 200);
                parameterArray2[0].Value = this.ViewState["CodeFlag"].ToString();
                parameterArray2[1] = new SqlParameter("@CodeNo", SqlDbType.NVarChar, 200);
                parameterArray2[1].Value = this.txtCodeNo.Text;
                DataTable table2 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, str3, parameterArray2).Tables[0];
                if (table2.Rows.Count > 0)
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPrompt("编号已经存在！", "new.aspx?code_flag=" + this.ViewState["CodeFlag"].ToString());
                }
                else
                {
                    str3 = "INSERT INTO [dbo].[Sys_Code](CodeNo,CodeName,CodeFlag) VALUES(@CodeNo,@CodeName,@CodeFlag)";
                    parameterArray2 = new SqlParameter[3];
                    parameterArray2[0] = new SqlParameter("@CodeNo", SqlDbType.NVarChar, 200);
                    parameterArray2[0].Value = this.txtCodeNo.Text;
                    parameterArray2[1] = new SqlParameter("@CodeName", SqlDbType.NVarChar, 200);
                    parameterArray2[1].Value = this.txtCodeName.Text;
                    parameterArray2[2] = new SqlParameter("@CodeFlag", SqlDbType.NVarChar, 200);
                    parameterArray2[2].Value = this.ViewState["CodeFlag"].ToString();
                    SqlHelper.ExecuteNonQuery(base.Conn, CommandType.Text, str3, parameterArray2);
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPrompt("代码增加成功！", "func.aspx?code_no=" + this.ViewState["CodeFlag"].ToString());
                }
            }
        } 

    }
}
